package com.ityunhe.sevlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ityunhe.bean.Account;
import com.ityunhe.bean.Garden;
import com.ityunhe.biz.GardenAccountBis;
import com.ityunhe.biz.LogOutBis;
import com.ityunhe.biz.UserAccountBis;

@WebServlet("/home/logOutServelt")
public class LogOutServelt extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public LogOutServelt() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LogOutBis logOutBis = new LogOutBis();
		HttpSession session = request.getSession();
		String opr = request.getParameter("opr");
		if ("login".equals(opr)) {
			if(session.getAttribute("login")!=null){
				session.removeAttribute("login");
			}
			String username = request.getParameter("username");
			String userpwd = request.getParameter("userpwd");
			String[] role = request.getParameterValues("role");// 角色
			String[] remember = request.getParameterValues("remember");// 是否记住密码			
			// 如果是普通用户
			if ("account".equals(role[0])) {
				Account account = new Account();
				// 取得参数]
				account.setUsername(username);
				account.setUserpassword(userpwd);
				// 调用loign查询
				Account login =logOutBis.accountLogin(account);
				if (login != null) {
					// 将用户信息保存到session中
					session.setAttribute("login", login);
					// 如果设置了保存密码就
					if (remember != null) {
						Cookie cookie = new Cookie("login", username + ","
								+ userpwd);
						// 保存5天
						cookie.setMaxAge(60 * 60 * 24 * 5);
						response.addCookie(cookie);

					} else {
						Cookie[] cookies = request.getCookies();
						cookies[0].setMaxAge(0);
					}

					// 跳转
					response.sendRedirect(request.getContextPath()+"/home/home2.jsp");
					return;
				} else {
					// 出现错误 跳转回
					request.setAttribute("error", "登录失败");
					request.getRequestDispatcher("/home/login.jsp").forward(request, response);
					return;
				}
				// 菜农登录
			}else if("garden".equals(role[0])) {
				if(session.getAttribute("login")!=null){
					session.removeAttribute("login");
				}
				Garden garden = new Garden();
				garden.setGardenname(username);
				garden.setGardenpassword(userpwd);
				// 调用loign查询
				Garden login = logOutBis.gardenlogin(garden);
				if (login != null) {
					// 将用户信息保存到session中
					session.setAttribute("login", login);
					// 如果设置了保存密码就
					if (remember != null) {
						Cookie cookie = new Cookie("login", username + ","
								+ userpwd);
						// 保存5天
						cookie.setMaxAge(60 * 60 * 24 * 5);
						response.addCookie(cookie);
					} else {
						Cookie[] cookies = request.getCookies();
						cookies[0].setMaxAge(0);
					}
					// 跳转
					response.sendRedirect(request.getContextPath()+"/home/gardensmain.jsp");
					return;
				} else {
					// 出现错误 跳转回
					request.setAttribute("error", "登录失败");
					request.getRequestDispatcher("/home/login.jsp").forward(request, response);
					return;
				}

			}

		}else if(opr==null||"".equals(opr)){
			request.getRequestDispatcher("/home/login.jsp").forward(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
